/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.saar.penguin.give2.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * This class describes a room which is used to compute messages like
 * "This is the room with the chair, remember it"
 *
 * @author Boris Fersing
 */
public class PathRoom {

    //The list of object types used for the instruction
    private ObjectType[] targetTypes = {ObjectType.BED, ObjectType.DRESSER, ObjectType.TABLE,
        ObjectType.COUCH, ObjectType.CHAIR, ObjectType.PICTURE, ObjectType.FLOWER,
        ObjectType.LAMP, ObjectType.WINDOWLONG, ObjectType.WINDOWWIDE};//, ObjectType.SAFE};
    //The describedObject corresponding to the current room
    private DescribedObject currentRoom;
    //The content of the current room
    private Set<DescribedObject> roomObjects;
    //all the content of all rooms that have been already visited
    private Set<DescribedObject> historyObjects;
    //all the room the player has already visited when he enters the current room
    private Set<DescribedObject> historyRooms;
    //does the current room contain manipulate actions?
    private boolean manipulate;

    //The room descriptions
    List<RoomDescription> descriptions;

    public PathRoom(DescribedObject currentRoom, Set<DescribedObject> historyRooms,
            Set<DescribedObject> historyObjects, boolean manipulate) {
        this.currentRoom = currentRoom;
        this.roomObjects = new TreeSet<DescribedObject>(currentRoom.getRoomContents());
        this.historyObjects = historyObjects;
        this.historyRooms = historyRooms;
        this.manipulate = manipulate;
    }

    /**
     * This method returns the message used to refer to the room (e.g. The room with the couch)
     * @return the message if the room can be identified using objects, empty string otherwise
     */
    public List<RoomDescription> getDescriptions() {

        descriptions = new ArrayList<RoomDescription>();

        /*
         * Check for distinctive objects
         */

        // First remove the current room's objects from the history (if any)

        historyObjects.removeAll(roomObjects);

        // Build a list with all types in the room
        List<ObjectType> roomTypes = new ArrayList<ObjectType>();

        for (DescribedObject obj : roomObjects) {
            roomTypes.add(obj.getType());
        }

        // Now build a list with all types in the history
        List<ObjectType> historyTypes = new ArrayList<ObjectType>();

        for (DescribedObject obj : historyObjects) {
            historyTypes.add(obj.getType());
        }

        // Now remove from the roomTypes all the types present in the history
        roomTypes.removeAll(historyTypes);

        // If roomTypes is now empty, the room doesn't contain any distinctive object
        // We can skip to the wallpapers part
        if (!roomTypes.isEmpty()) {

            // roomTypes is not empty, check if the room contains some targetTypes
            for (ObjectType type : targetTypes) {
                descriptions.add(new RoomDescription(currentRoom, type));
            }
        }

        // If descriptions is not empty we don't need to process the room colors
        if (!descriptions.isEmpty())
            return descriptions;

        /*
         * Check for a distinctive wallpaper color
         */

        // first remove the current room from the history
        historyRooms.remove(currentRoom);

        //The build a set of the colors already seen in the history
        Set<ObjectColor> historyColors = new TreeSet<ObjectColor>();
        for (DescribedObject room : historyRooms) {
            historyColors.add(room.getColor());
        }

        //If the current rooms color exists in the history we can't use the
        //wallpapers color to refer to it, otherwise, use it
        if (!historyColors.contains(currentRoom.getColor())) {
            descriptions.add(new RoomDescription(currentRoom));
        }

        return descriptions;
    }

    /**
     * Can the room be identified using objects (couch, lamp...) or wallpapers?
     * @return true if it's the case, false otherwise
     */
    public boolean isDistinctive() {
        if (getDescriptions().isEmpty()) {
            return false;
        }
        return true;
    }

    public boolean isManipulate() {
        return manipulate;
    }

    public void setManipulate(boolean manipulate) {
        this.manipulate = manipulate;
    }

    public String getId() {
        return currentRoom.getId();
    }

    public Set<DescribedObject> getHistoryObjects() {
        return historyObjects;
    }

    /**
     * Checks if the current room is present in a list of rooms
     * @param rooms
     * @return
     */
    public boolean isInList(List<PathRoom> rooms) {
        for (PathRoom room : rooms) {
            if (room.getId().equals(this.getId())) {
                return true;
            }
        }
        return false;
    }
}
